package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.BatchQueryHostTypeReq;
import com.sg.dto.base.framework.req.QueryHostTypeDetailReq;
import com.sg.dto.base.framework.req.QueryHostTypeListReq;
import com.sg.entity.HostType;
import com.sg.service.base.framework.MHostTypeService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.HostTypeMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MHostTypeServiceImpl
        extends ServiceImpl<HostTypeMapper, HostType>
        implements MHostTypeService {

    @Autowired
    HostTypeMapper hostTypeMapper;

    /**
     * 1-1-14查询宿主类型详情[195]
     * gen by moon at 8/29/2022, 11:58:54 PM
     */
    @Trace(operationName = "1-1-14查询宿主类型详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public HostType queryHostTypeDetail(QueryHostTypeDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<HostType> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<HostType> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                HostType retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-14查询宿主类型列表[104]
     * gen by moon at 8/29/2022, 11:58:54 PM
     */
    @Trace(operationName = "1-1-14查询宿主类型列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<HostType> queryHostTypeList(QueryHostTypeListReq reqDto) {

        try {
            QueryWrapper<HostType> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(HostType.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(HostType.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(HostType.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<HostType>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-14删除宿主类型[103]
     * gen by moon at 8/29/2022, 11:58:53 PM
     */
    @Trace(operationName = "1-1-14删除宿主类型")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteHostType(String hostTypeId) {

        return super.removeById(hostTypeId);
    }

    /**
     * 1-1-14修改宿主类型[102]
     * gen by moon at 8/29/2022, 11:58:53 PM
     */
    @Trace(operationName = "1-1-14修改宿主类型")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateHostType(HostType hostType) {
        hostType.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (hostType != null && hostType.getSubjectLifeCycle() != null && hostType.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            hostType.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (hostType != null && hostType.getSubjectLifeCycle() != null && hostType.getSubjectLifeCycle().equals("ARCHIVING")) {
            hostType.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        hostType.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (hostType != null && hostType.getSubjectLifeCycle() != null && hostType.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            hostType.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (hostType != null && hostType.getSubjectLifeCycle() != null && hostType.getSubjectLifeCycle().equals("ARCHIVING")) {
            hostType.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(hostType);
    }

    /**
     * 1-1-14新增宿主类型[101]
     * gen by moon at 10/25/2022, 9:51:10 AM
     */
    @Trace(operationName = "1-1-14新增宿主类型")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addHostType(HostType hostType) {
        hostType.setHostTypeId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        hostType.setIsRecommend("FALSE");
        hostType.setIsFreeze("FALSE");
        hostType.setTempFreezeStatus("FALSE");
        hostType.setIsArchive("FALSE");
        hostType.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        hostType.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (hostType != null && hostType.getSubjectLifeCycle() != null && hostType.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            hostType.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        hostType.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        hostType.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (hostType != null && hostType.getSubjectLifeCycle() != null && hostType.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            hostType.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        hostType.setIsValid("FALSE");
        hostType.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        hostType.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(hostType)) {
            return hostType.getHostTypeId();
        } else {
            return "-1";
        }

    }

    /**
     * 1-1-14批量查询宿主类型[5006]
     * gen by moon at 10/25/2022, 9:51:13 AM
     */
    @Trace(operationName = "1-1-14批量查询宿主类型")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<HostType> batchQueryHostType(BatchQueryHostTypeReq reqDto) {

        try {
            QueryWrapper<HostType> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getHostTypeList() == null || reqDto.getHostTypeList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("host_type_id", reqDto.getHostTypeList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(HostType.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(HostType.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(HostType.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<HostType>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
